package com.mnc.web.action.manage;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mnc.entity.Article;
import com.mnc.entity.ArticleType;
import com.mnc.entity.User;
import com.mnc.entity.UserType;
import com.mnc.util.Helper;
import com.mnc.util.MD532;
import com.mnc.util.ServiceUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class ManageAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3592604238773581335L;
	private @Resource ServiceUtil serviceUtil;
	
	private int userId;
	private String loginName;
	private String loginPass;
	private int loginType;
	private String returnMsg;
	
	private ArticleType articleType;
	
	@JSON(serialize=false)
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	@JSON(serialize=false)
	public ServiceUtil getServiceUtil() {
		return serviceUtil;
	}
	public void setServiceUtil(ServiceUtil serviceUtil) {
		this.serviceUtil = serviceUtil;
	}
	@JSON(serialize=false)
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	@JSON(serialize=false)
	public String getLoginPass() {
		return loginPass;
	}
	public void setLoginPass(String loginPass) {
		this.loginPass = loginPass;
	}
	@JSON(serialize=false)
	public int getLoginType() {
		return loginType;
	}
	public void setLoginType(int loginType) {
		this.loginType = loginType;
	}
	@JSON(serialize=false)
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	@JSON(serialize=false)
	public ArticleType getArticleType() {
		return articleType;
	}
	public void setArticleType(ArticleType articleType) {
		this.articleType = articleType;
	}
	
	public String getReturnMsg() {
		return returnMsg;
	}
	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	
	@Override
	public String execute() throws Exception {
		return "login";
	}
	//管理员登陆
	public String doLogin() {
		if (null!=loginName&&loginName.trim().length()>0&&null!=loginPass&&loginPass.trim().length()>0) {
			User loginUser = this.getServiceUtil().getUserService().userLogin(loginName, loginPass);
			if (null!=loginUser) {
				ActionContext.getContext().getSession().put(Helper.MANAGE_LOGIN, loginUser);
				return SUCCESS;
			}
			this.addFieldError("error", "请输入正确的登录信息！");
			return "login";
		}
		this.addFieldError("error", "请输入必要的登录信息！");
		return "login";
	}
	public String buildMenu() {
		return SUCCESS;
	}
	//显示板块（文章分类）信息
	public String showArticleTypes() {
		List<ArticleType> data = this.getServiceUtil().getArticleTypeService().getData(ArticleType.class);
		long[] counts=new long[data.size()];
		for (int i=0;i<data.size();i++) {
			long count = this.getServiceUtil().getArticleService().getCount(Article.class, Helper.WHERE_ARTICLE_TYPE, new Object[] {data.get(i).getTypeId()});
			counts[i]=count;
		}
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("count", counts);
		return "ok";
	}
	//修改板块
	public String editArticleType() {
		if (null!=this.getArticleType()) {
			this.getServiceUtil().getArticleTypeService().update(this.getArticleType());
			this.setReturnMsg("板块修改成功！");
		}else {
			this.setReturnMsg("服务器未响应，请重新修改！");
		}
		return "ok";
	}
	//添加板块
	public String addArticleType() {
		if (null!=this.getArticleType()) {
			this.getServiceUtil().getArticleTypeService().add(this.getArticleType());
			this.setReturnMsg("板块添加成功！");
		}else {
			this.setReturnMsg("服务器未响应，请重新添加！");
		}
		return "ok";
	}
	//显示后台用户
	public String showManageUsers() {
		List<User> data = this.getServiceUtil().getUserService().getData(User.class, Helper.WHERE_SHOW_MANAGE_USER, null);
		ActionContext.getContext().put("data", data);
		return "ok";
	}
	//添加后台用户
	public String addManageUser() {
		if(null!=loginName&&loginName.trim().length()>0&&null!=loginPass&&loginPass.trim().length()>0&&loginType>0) {
			UserType type=new UserType();
			User user=new User();
			type.setTypeId(loginType);
			user.setMyType(type);
			user.setUserLoginName(loginName);
			user.setUserLoginPass(new MD532().getMD5(loginPass));
			this.getServiceUtil().getUserService().add(user);
		}else {
			ActionContext.getContext().put("message", "请输入完整的用户信息！");
			return "empty";
		}
		return "ok";
	}
//	删除后台用户
	public String rmManageUser() {
		if (userId>0) {
			this.getServiceUtil().getUserService().remove(User.class, userId);
			this.setReturnMsg("用户删除成功！");
		}else {
			this.setReturnMsg("服务器未响应，请重新添加！");
		}
		return "ok";
	}

}